#include <Windows.h>
#include <tchar.h>

//////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{	
	static const UINT ID_BTNMOVE	= 1001;
	static const UINT ID_BTNSTATE	= 1002;
	static HWND hBtnWndMove			= NULL;
	static HWND hBtnWndState		= NULL;

	switch (nMsg)
	{
		case WM_CREATE:
		{
			hBtnWndMove	= CreateWindow(_T("Button"), _T("Move Center"), WS_VISIBLE|WS_CHILD, 0, 0, 100, 50, hWnd, (HMENU)ID_BTNMOVE, 0, 0);
			hBtnWndState = CreateWindow(_T("Button"), _T("To TopMost"), WS_VISIBLE|WS_CHILD, 150, 0, 200, 50, hWnd, (HMENU)ID_BTNSTATE, 0, 0); 
	
			break;
		}
		case WM_COMMAND:
		{
			switch (wParam)
			{
				case ID_BTNMOVE:
				{
					UINT nSrnWidth		= GetSystemMetrics(SM_CXFULLSCREEN);
					UINT nSrnHeight		= GetSystemMetrics(SM_CYFULLSCREEN);
					
					//--: Move Main wnd to center of screen
					RECT rtMainWnd;
					GetClientRect(hWnd, &rtMainWnd);
					UINT nWndWidth		= (rtMainWnd.right - rtMainWnd.left);
					UINT nWndHeight		= (rtMainWnd.bottom - rtMainWnd.top);
					UINT nLeft			= (nSrnWidth - nWndWidth) / 2;
					UINT nTop			= (nSrnHeight - nWndHeight) / 2;
					SetWindowPos(hWnd, NULL, nLeft, nTop, nWndWidth, nWndHeight, SWP_NOSIZE|SWP_NOZORDER);

					//--: Move "Move Center" Button to center of client area and inflate
					RECT rtBtnMove;
					GetClientRect(hBtnWndMove, &rtBtnMove);
					UINT nMoveBtnWidth	= 200;
					UINT nMoveBtnHeight = 100;
					UINT nX				= (nWndWidth - nMoveBtnWidth) / 2;
					UINT nY				= (nWndHeight - nMoveBtnHeight) / 2;
					SetWindowPos(hBtnWndMove, NULL, nX, nY, nMoveBtnWidth, nMoveBtnHeight, SWP_NOZORDER);

					break;
				}
				case ID_BTNSTATE:
				{
					static bool bTopMost = false;
					HWND hWndInsertAfter;
					if (bTopMost)
					{
						hWndInsertAfter = HWND_NOTOPMOST;   // HWND_BOTTOM; //HWND_TOP;
						SetWindowText(hBtnWndState, _T("To TopMost"));
						bTopMost = false;
					} else {
						hWndInsertAfter = HWND_TOPMOST;
						SetWindowText(hBtnWndState, _T("To NoTopMost"));
						bTopMost = true;
					}
					SetWindowPos(hWnd, hWndInsertAfter, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);

					break;
				}
			}
			break;
		}
		case WM_DESTROY:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
}

//////////////////////////////////////////////////////////////////////////
bool RegisterWndClass(HINSTANCE hInstance, const TCHAR* szClsName)
{
	WNDCLASSEX wcs		= {0};
	wcs.cbSize			= sizeof(WNDCLASSEX);
	wcs.hbrBackground	= (HBRUSH)GetStockObject(COLOR_APPWORKSPACE);
	wcs.lpszClassName	= szClsName;
	wcs.lpfnWndProc		= WndProc;
	wcs.hCursor			= LoadCursor(NULL, MAKEINTRESOURCE(IDC_ARROW));
	wcs.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
	wcs.style			= CS_VREDRAW|CS_HREDRAW;
	
	return (0 != RegisterClassEx(&wcs));
}

//////////////////////////////////////////////////////////////////////////
bool CreateMainWnd(HINSTANCE hInstance, const TCHAR* szClsName, const TCHAR* szWndCaption)
{
	if (NULL != szClsName && NULL != szWndCaption)
	{
		HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, szClsName, szWndCaption, WS_VISIBLE|WS_POPUPWINDOW|WS_CAPTION, 
									0, 0, 800, 480, NULL, NULL, hInstance, hInstance);
		if (NULL != hWnd && IsWindow(hWnd))
		{
			ShowWindow(hWnd, SW_SHOW);
			UpdateWindow(hWnd);
			return true;
		}
	}
	return false;
}

//////////////////////////////////////////////////////////////////////////
void RunMessageLoop()
{
	MSG Msg;
	while (true)
	{
		while (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
		{
			if (WM_QUIT == Msg.message)
			{
				return;
			}

			TranslateMessage(&Msg);
			DispatchMessage(&Msg);
		}
	}
}
//////////////////////////////////////////////////////////////////////////
BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	const TCHAR* szClsName		= _T("Win32App");
	const TCHAR* szWndCaption	= _T("Demo TopMost Wnd");

	if (RegisterWndClass(hInstance, szClsName))
	{
		if (CreateMainWnd(hInstance, szClsName, szWndCaption))
		{
			RunMessageLoop();
		}
	}

	return TRUE;
}